﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Threading.Tasks;
using YiSha.Util;
using YiSha.Util.Extension;
using YiSha.Util.Model;
using YiSha.Entity.QixingManage;
using YiSha.Model.Param.QixingManage;
using YiSha.Service.QixingManage;
using YiSha.Model.Result.QixingManage;

namespace YiSha.Business.QixingManage
{
    /// <summary>
    /// 创 建：admin
    /// 日 期：2023-03-19 19:43
    /// 描 述：商品规格库存表业务类
    /// </summary>
    public class GoodsWarehouseBLL
    {
        private GoodsWarehouseService goodsWarehouseService = new GoodsWarehouseService();
        
        #region 获取数据
        public async Task<TData<List<GoodsWarehouseEntity>>> GetList(GoodsWarehouseListParam param)
        {
            TData<List<GoodsWarehouseEntity>> obj = new TData<List<GoodsWarehouseEntity>>();
            obj.Data = await goodsWarehouseService.GetList(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData<List<GoodsWarehouseEntity>>> GetPageList(GoodsWarehouseListParam param, Pagination pagination)
        {
            TData<List<GoodsWarehouseEntity>> obj = new TData<List<GoodsWarehouseEntity>>();
            obj.Data = await goodsWarehouseService.GetPageList(param, pagination);
            obj.Total = pagination.TotalCount;
            obj.Tag = 1;
            return obj;
        }

        public TData<List<goodscar>> GetSelectCarListJson(GoodsWarehouseListParam param)
        {
            TData<List<goodscar>> obj = new TData<List<goodscar>>();
            obj.Data =goodsWarehouseService.GetSelectCarListJson(param);
            obj.Total = obj.Data.Count;
            obj.Tag = 1;
            return obj;
        }
        

        public async Task<TData<GoodsWarehouseEntity>> GetEntity(long id)
        {
            TData<GoodsWarehouseEntity> obj = new TData<GoodsWarehouseEntity>();
            obj.Data = await goodsWarehouseService.GetEntity(id);
            if (obj.Data != null)
            {
                obj.Tag = 1;
            }
            return obj;
        }
        #endregion

        #region 提交数据
        public async Task<TData<string>> SaveForm(GoodsWarehouseEntity entity)
        {
            TData<string> obj = new TData<string>();

            //判断车架号是否存在
            var count=goodsWarehouseService.GetVINEntity(entity);

            if (count > 0) {
                obj.Tag = 0;
                obj.Message ="车架号已存在";
                return obj;
            }

            //if (entity.Id.IsNullOrZero()) {
            //    //增加颜色的库存
            //    var color = goodsWarehouseService.BaseRepository().FindList<GoodsAttrEntity>(t => t.Id == entity.colorid).Result.FirstOrDefault();
            //    color.count = color.count + 1;
            //    await goodsWarehouseService.BaseRepository().Update(color);
            //}

            await goodsWarehouseService.SaveForm(entity);

            obj.Data = entity.Id.ParseToString();
            obj.Tag = 1;
            return obj;
        }

        public async Task<TData> DeleteForm(string ids)
        {
            TData obj = new TData();
            await goodsWarehouseService.DeleteForm(ids);
            obj.Tag = 1;
            return obj;
        }
        #endregion

        #region 私有方法
        #endregion
    }
}
